<template>
    <el-card shadow="never" class="border-0">
        <!-- 新增|刷新 -->
        <ListHeader @create="handleCreate" @refresh="getData"> </ListHeader>
        <el-tree :data="tableData" :props="{ label:'name',children:'child' }" v-loading="loading" node-key="id">
            <template #default="{ node, data }">
                <div class="custom-tree-node">
                    <span>{{ data.name }}</span>

                    <div class="ml-auto">
                        <el-button text type="primary" size="small" @click="handleGoodsDrawerRef(data)">推荐商品</el-button>

                        <el-switch :modelValue="data.status" :active-value="1" :inactive-value="0" @change="handleStatusChange($event,data)" />
                        <el-button text type="primary" size="small" @click.stop="handleEdit(data)">修改</el-button>

                        <el-popconfirm title="是否要删除该记录？" confirmButtonText="确认" cancelButtonText="取消" @confirm="handleDelete(data.id)">
                            <template #reference>
                                <el-button text type="primary" size="small">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </div>

                </div>
            </template>
        </el-tree>

        <FormDrawer ref="formDrawerRef" :title="drawerTitle" @submit="handleSubmit">
            <el-form :model="form" ref="formRef" :rules="rules" label-width="80px" :inline="false">
                <el-form-item label="分类名称" prop="title">
                    <el-input v-model="form.name" placeholder="请输入分类名称"></el-input>
                </el-form-item>
            </el-form>
        </FormDrawer>
        <GoodsDrawer ref="GoodsDrawerRef" />
    </el-card>
</template>
<script setup>
import { ref } from 'vue'
import GoodsDrawer from '~/pages/category/components/GoodsDrawer.vue'
import FormDrawer from "~/components/FormDrawer.vue";
import ListHeader from "~/components/ListHeader.vue";
import { useInitTable, useInitForm } from "~/composables/useCommon";

import {
    getCategoryList,
    createCategoryList,
    updateCategoryList,
    updateCategoryStatus,
    deleteCategoryList,
    deleteCategoryItem
} from "~/api/category.js"

const GoodsDrawerRef = ref(null)

const handleGoodsDrawerRef = (data) => {
    GoodsDrawerRef.value.open(data)
}

const {
    searchForm,
    resetSearchForm,
    tableData,
    loading,
    currentPage,
    total,
    limit,
    getData,
    handleDelete,
    handleStatusChange,
    multiSelectionIds,
    handleSelectionChange,
    handleMult,
    handleMultiStatusChange,
    multipleTableRef
} = useInitTable({
    getList: getCategoryList,
    delete: deleteCategoryList,
    updateStatus: updateCategoryStatus,
    onGetListSuccess: (res) => {
        tableData.value = res.map((item) => {
            item.statusLoading = false
            return item
        })
    }
})

const { formDrawerRef,
    formRef,
    form,
    rules,
    drawerTitle,
    handleSubmit,
    handleCreate,
    handleEdit } = useInitForm(
        {
            form: {
                name: ''
            },
            rules: {
                name: [
                    { required: true }
                ]
            },
            getData,
            update: updateCategoryList,
            create: createCategoryList,
        })
</script>
<style>
.custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    font-size: 14px;
    padding-right: 8px;
}
.el-tree-node__content {
    padding: 20px 0;
}
</style>